const http = require('http');
const querystring = require('querystring');
const getPostData = require('./util/request.js');
const handlerRouter = require('./router/router.js');

const PORT = 8080;
// 创建server服务
const server = http.createServer(async (req, res) => {
    // 获取请求的路径
    const url = req.url
    // 获取接口路径
    req.path = url.split('?')[0];
    // 解析query,获取get请求参数
    req.query = querystring.parse(url.split('?')[1]);
    const responseData = await getPostData(req)
    req.body = responseData;
    handlerRouter(req, res);
})
// 监听8080端口
server.listen(PORT, () => {
    console.log(`server is running at port ${PORT}`);
});